package com.hup.sidebar.model;

import android.content.Context;
import android.graphics.Color;

import com.alibaba.fastjson.annotation.JSONField;
import com.hup.sidebar.framework.model.BaseConfig;
import com.hup.sidebar.framework.model.SpFieldHandler.SpBooleanHandler;
import com.hup.sidebar.framework.model.SpFieldHandler.SpField;
import com.hup.sidebar.framework.model.SpFieldHandler.SpIntHandler;
import com.hup.sidebar.framework.model.SpFieldHandler.SpStringHandler;
import com.hup.sidebar.framework.model.SpFieldHandler.SpStringListHandler;
import com.hup.sidebar.framework.util.ColorUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author hugan
 * @date 2022/6/3
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class AppConfig extends BaseConfig {

    //app状态属性
    /**
     * 悬浮窗服务是否开启
     */
    @JSONField(serialize = false)
    public boolean floaterStarted;
    /**
     * 无障碍服务是否开启
     */
    @JSONField(serialize = false)
    public boolean accessibilityStarted;

    //一般配置
    /**
     * 开机启动
     */
    @JSONField(ordinal = 1)
    @SpField(handler = SpBooleanHandler.class)
    public boolean autoStart;

    /**
     * 白名单,app包名
     *
     * @apiNote 未使用
     */
    @JSONField(serialize = false)
    public ArrayList<String> whiteList = new ArrayList<>();

    //===激活器配置
    @JSONField(ordinal = 2)
    @SpField(defInt = 30, handler = SpIntHandler.class)
    public int activeWidth;
    /**
     * 激活器高度,横竖屏一样
     */
    @JSONField(ordinal = 3)
    @SpField(defInt = 500, handler = SpIntHandler.class)
    public int activeHeight;
    /**
     * 竖直居中的y偏移量,横竖屏一样
     */
    @JSONField(ordinal = 4)
    @SpField(handler = SpIntHandler.class)
    public int activeYOffset;

    //===功能区配置
    /**
     * 功能
     * 缺省功能:4个扇区,中间是返回和home键
     */
    @JSONField(ordinal = 9)
    @SpField(defString = "[{},{\"longPress\":{\"command\":\"Home键\",\"type\":2},\"normal\":{\"command\":\"返回键\",\"type\":2}},{\"normal\":{\"command\":\"返回键\",\"type\":2}},{}]",
            listItemClz = MultiPieFunction.class, handler = SpStringListHandler.class)
    public ArrayList<MultiPieFunction> functions = new ArrayList<>();

    /**
     * 扇形背景色
     */
    @JSONField(ordinal = 10)
    @SpField(defString = "#E0607D8B", handler = SpStringHandler.class)
    public String pieColorStr;
    @JSONField(serialize = false)
    public int pieColor;
    @JSONField(serialize = false)
    public int pieColorSelected;

    /**
     * 文本颜色
     */
    @JSONField(ordinal = 11)
    @SpField(defString = "#FFFFFF", handler = SpStringHandler.class)
    public String pieColorFontStr;
    @JSONField(serialize = false)
    public int pieColorFont;
    /**
     * 扇形外半径,px
     */
    @JSONField(ordinal = 12)
    @SpField(defInt = 230, handler = SpIntHandler.class)
    public int pieRadius;
    /**
     * 扇形的宽度=外半径-内半径
     */
    @JSONField(ordinal = 13)
    @SpField(defInt = 60, handler = SpIntHandler.class)
    public int pieWidth;
    /**
     * 扇形之间的间隙,单位:度
     */
    @JSONField(ordinal = 14)
    @SpField(defInt = 1, handler = SpIntHandler.class)
    public int pieGap;
    /**
     * 图标比例,[50,200]%
     */
    @JSONField(ordinal = 15)
    @SpField(defInt = 100, handler = SpIntHandler.class)
    public int pieIconRatio;
    /**
     * 长按时长,ms
     */
    @JSONField(ordinal = 16)
    @SpField(defInt = 260, handler = SpIntHandler.class)
    public int pieLongPressTime;
    /**
     * 长按时震动
     */
    @JSONField(ordinal = 17)
    @SpField(defBoolean = true, handler = SpBooleanHandler.class)
    public boolean pieVibrate;
    /**
     * 长按震动时长,ms
     * 0-使用系统默认效果
     */
    @JSONField(ordinal = 18)
    @SpField(handler = SpIntHandler.class)
    public int vibrateTime;
    /**
     * 显示触摸时的指示器:目前是一条线
     */
    @JSONField(ordinal = 19)
    @SpField(handler = SpBooleanHandler.class)
    public boolean pieShowIndicator;

    //===便签
    /**
     * 显示便签
     */
    @JSONField(ordinal = 30)
    @SpField(defBoolean = true, handler = SpBooleanHandler.class)
    public boolean noteShow;
    /**
     * 字体大小,sp
     */
    @JSONField(ordinal = 31)
    @SpField(defInt = 20, handler = SpIntHandler.class)
    public int noteFontSize;
    /**
     * 显示日期
     */
    @JSONField(ordinal = 32)
    @SpField(handler = SpBooleanHandler.class)
    public boolean noteDateShow;
    @JSONField(ordinal = 33)
    @SpField(defString = "yyyy-MM-dd EEE", handler = SpStringHandler.class)
    public String noteDateFormat;
    @JSONField(serialize = false)
    public SimpleDateFormat noteDateFormatter;

    public static final int NOTE_MAX_LENGTH = 800;
    /**
     * 便签数据
     */
    //@SpField(handler = SpStringHandler.class)//独立处理
    @JSONField(serialize = false)
    public String noteData;

    //==临时关闭
    @JSONField(ordinal = 35)
    @SpField(handler = SpBooleanHandler.class)
    public boolean tempHideShow;

    //==app跳转
    /**
     * true-使用 TrampolineActivity 跳转app
     */
    @JSONField(ordinal = 36)
    @SpField(handler = SpBooleanHandler.class)
    public boolean useTrampoline;

    public void loadNoteData(Context context) {
        noteData = getSp(context).getString("noteData", "");
    }

    public void saveNoteData(Context context) {
        getSp(context).edit().putString("noteData", noteData).apply();
    }

    public void applyPieColor() {
        pieColor = Color.parseColor(pieColorStr);
        pieColorSelected = ColorUtil.darker(pieColor);
        pieColorFont = Color.parseColor(pieColorFontStr);
    }

    public String simpleString() {
        return "AppConfig{" +
                "autoStart=" + autoStart +
                ", activeWidth=" + activeWidth +
                ", activeHeight=" + activeHeight +
                ", activeYOffset=" + activeYOffset +
                ", pieColorStr=" + pieColorStr +
                ", pieRadius=" + pieRadius +
                ", pieWidth=" + pieWidth +
                ", pieGap=" + pieGap +
                ", pieIconRatio=" + pieIconRatio +
                ", pieLongPressTime=" + pieLongPressTime +
                ", pieVibrate=" + pieVibrate +
                ", vibrateTime=" + vibrateTime +
                ", pieShowIndicator=" + pieShowIndicator +
                //
                ", noteShow=" + noteShow +
                ", noteFontSize=" + noteFontSize +
                ", noteDateShow=" + noteDateShow +
                ", noteDateFormat=" + noteDateFormat +
                //
                ", tempHideShow=" + tempHideShow +
                ", useTrampoline=" + useTrampoline +
                '}';
    }

}
